home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / pdcurs21.zip / NONPORT.ZIP / RESIZEW.C < prev    next >
Text File  |  1992-11-21  |  5KB  |  140 lines

  1. #ifndef NO_MEMORY_H
  2. #include <memory.h>
  3. #endif
  4. #define        CURSES_LIBRARY  1
  5. #include <curses.h>
  6. #undef resize_win
  7.  
  8. #ifndef NDEBUG
  9. char *rcsid_resizew = "$Header: c:/curses/nonport/RCS/resizew.c%v 2.0 1992/11/15 03:18:28 MH Rel $";
  10. #endif
  11.  
  12.  
  13.  
  14.  
  15. /*man-start*********************************************************************
  16.  
  17.   resize_win() -       Resize a window
  18.  
  19.   PDCurses Description:
  20.        Resizes the passed WINDOW* to reflect the maximum size
  21.        represented by the WINDOW* fields _pmaxy and _pmaxx.
  22.  
  23.        If _maxy < _pmaxy then new lines will be added.
  24.        The same is also true for _maxx and _pmaxx.
  25.  
  26.        WARNING TO PROGRAMMERS:
  27.  
  28.        This function assumes that when a window is enlarged
  29.        horizontally, the contents on each existing line will be
  30.        copied to the new, enlarged line and the remainder of the
  31.        enlarged line will be cleared.
  32.  
  33.        When a window is enlarged vertically, each new line added
  34.        will be blank.
  35.  
  36.        All borders will be observed and enlarged appropriately.
  37.  
  38.        When a windows are shrunk, only the WINDOW* fields _pmaxy and
  39.        _pmaxx are affected.
  40.  
  41.   PDCurses Return Value:
  42.        The resize_win() function returns a NULL pointer or a valid
  43.        WINDOW* which may or may not point to the same physical
  44.        window.  Besure to change all pointers to the passed window
  45.        to the address returned by this function (but only if it is
  46.        non-zero).
  47.  
  48.   PDCurses Errors:
  49.        It is an error to pass a NULL WINDOW pointer.
  50.  
  51.   Portability:
  52.        PDCurses        WINDOW* resize_win( WINDOW* w, int lines, int cols );
  53.  
  54. **man-end**********************************************************************/
  55.  
  56. WINDOW*        resize_win(WINDOW *w, int lines, int cols)
  57. {
  58. extern void*   (*mallc)();     /* ptr to some malloc(size)     */
  59. extern void*   (*callc)();     /* ptr to some ecalloc(num,size)*/
  60. extern void    (*fre)();       /* ptr to some free(ptr)        */
  61.  
  62.        WINDOW* new;
  63.        int     ncols  = max(cols, w->_pmaxx);
  64.        int     nlines = max(lines, w->_pmaxy);
  65.        int     i;
  66.        int     j;
  67.  
  68.        if (w == (WINDOW *)NULL)
  69.                return( (WINDOW *)NULL );
  70.  
  71.        if ((lines > w->_pmaxy) || (cols > w->_pmaxx))
  72.        {
  73.                if ((new = PDC_makenew(nlines, ncols, w->_begy, w->_begx)) == (WINDOW *)NULL)
  74.                        return( (WINDOW *)NULL );
  75.  
  76.                new->_curx = w->_curx;
  77.                new->_cury = w->_cury;
  78.                new->_flags = w->_flags;
  79.                new->_attrs = w->_attrs;
  80.                new->_tabsize = w->_tabsize;
  81.                new->_clear = w->_clear;
  82.                new->_leave = w->_leave;
  83.                new->_scroll = w->_scroll;
  84.                new->_nodelay = w->_nodelay;
  85.                new->_use_keypad = w->_use_keypad;
  86.                new->_tmarg = w->_tmarg;
  87.                new->_bmarg = w->_bmarg;
  88.                new->_title = w->_title;
  89.                new->_title_ofs = w->_title_ofs;
  90.                new->_title_attr = w->_title_attr;
  91.                new->_parent = w->_parent;
  92.                memcpy(new->_borderchars, w->_borderchars, 8*sizeof(chtype));
  93.  
  94.                for (i = 0; i < nlines; i++)
  95.                {
  96.                        /*
  97.                         * make and clear the lines
  98.                         */
  99.                        if ((new->_y[i] = (chtype*)(*callc)(ncols, sizeof(chtype))) == NULL)
  100.                        {
  101.                                for (j = 0; j < i; j++)
  102.                                {
  103.                                        /*
  104.                                         * if error, free all the data
  105.                                         */
  106.                                        (*fre)(new->_y[j]);
  107.                                }
  108.                                (*fre)(new->_firstch);
  109.                                (*fre)(new->_lastch);
  110.                                (*fre)(new->_y);
  111.                                (*fre)(new);
  112.                                return( (WINDOW *)NULL );
  113.                        }
  114.                }
  115.                if ((w != curscr) && (w != tmpwin))
  116.                {
  117.                        overwrite(w, new);
  118.                        wmove(new, w->_maxy - 1, 0);
  119.                        wclrtobot(new);
  120. /* JGB box uses defaults if arguments are zero, but we don't want to do
  121.    this if the window currently has no box */
  122.                        if (w->_borderchars[0] || w->_borderchars[2])
  123.                                box(new, w->_borderchars[0], w->_borderchars[2]);
  124.                }
  125.                delwin(w);
  126.                return( new );
  127.        }
  128.        else
  129.        {
  130.                w->_bmarg = lines - 1;
  131.                w->_maxy = lines;
  132.                w->_maxx = cols;
  133. /* JGB box uses defaults if arguments are zero, but we don't want to do
  134.    this if the window currently has no box */
  135.                if (w->_borderchars[0] || w->_borderchars[2])
  136.                        box(w, w->_borderchars[0], w->_borderchars[2]);
  137.                return( w );
  138.        }
  139. }
  140.